import { asyncRoutes, constantRoutes } from '@/router'
import Layout from '@/layout'
import store from '@/store/index'

//导入开发组件
import viewsPlatformBulletinManageindex from '@/views/Platform/BulletinManage/index.vue'
import viewsPlatformButtonindex from '@/views/Platform/Button/index.vue'
import viewsPlatformMenuindex from '@/views/Platform/Menu/index.vue'
import viewsPlatformPermissionindex from '@/views/Platform/Permission/index.vue'
import viewsPlatformplatformPermissionsindex from '@/views/Platform/platformPermissions/index.vue'
import viewsPlatformRolesindex from '@/views/Platform/Roles/index.vue'
import viewsPlatformUsersindex from '@/views/Platform/Users/index.vue'
import viewsPlatformDept from '@/views/Platform/Dept/index.vue'
import viewsAppPermission from "@/views/Platform/appPermissions/index.vue"
import viewsMessageType from "@/views/Platform/MessageType/index.vue"
import viewsPloatformPage from '@/views/Platform/PageConfig/index.vue'
import viewsUserMessage from '@/views/Platform/UserMessage/index.vue'

//基础档案
import viewsBaseProduct from "@/views/BaseDoc/Product/index.vue"
import viewsBaseCustomer from "@/views/BaseDoc/Customer/index.vue"
import viewsBaseProcess from "@/views/BaseDoc/Process/index.vue"
import viewsBaseSupplier from "@/views/BaseDoc/Supplier/index.vue"

//项目管理
import viewsProjectInfo from "@/views/Project/Info/index.vue"
import viewsProjectScheduling from "@/views/Project/Scheduling/index.vue"
import viewsProjectExecution from "@/views/Project/Execution/index.vue"
import viewsProjectReceive from "@/views/Project/Receive/index.vue"
import viewsProjectInvoice from "@/views/Project/Invoice/index.vue"
import viewsProjectFinance from "@/views/Project/Finance/index.vue"

//客户管理
import viewsCustomerFollowUp from "@/views/Customer/FollowUp/index.vue"
import viewsCustomerSystems from "@/views/Customer/Systems/index.vue"
import viewsCustomerResume from "@/views/Customer/Resume/index.vue"

//渠道管理
import viewsSupplierResume from '@/views/Supplier/Resume/index.vue'

//证书管理
import viewsCertificateList from '@/views/Certificate/CertificateList/index.vue'
import viewsCertificateAnalysis from "@/views/Certificate/CertificateAnalysis/index.vue"

//报表中心
import viewsReportFinance from "@/views/Report/Finance/index.vue"
import viewsReportCustomer from "@/views/Report/Customer/index.vue"
import viewsReportWorktime from "@/views/Report/WorkTime/index.vue"

//文件中心
import viewsFileCenter from "@/views/File/index.vue"

import viewsTest1 from "@/views/Test/index.vue"


/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
    if (route.meta && route.meta.roles) {
        return roles.some(role => route.meta.roles.includes(role))
    } else {
        return true
    }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
    const res = []
    routes.forEach(route => {
        const tmp = { ...route }
        if (hasPermission(roles, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles)
            }
            res.push(tmp)
        }
    })

    return res
}

const state = {
    routes: [],
    addRoutes: []
}

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes)
    }
}

const routerMap = []
function CreateRouterMap() {
    if (routerMap && routerMap.length > 0) {
        return;
    }
    routerMap['Layout'] = Layout
    routerMap['/views/Platform/BulletinManage/index.vue'] = viewsPlatformBulletinManageindex
    routerMap['/views/Platform/Button/index.vue'] = viewsPlatformButtonindex
    routerMap['/views/Platform/Menu/index.vue'] = viewsPlatformMenuindex
    routerMap['/views/Platform/Permission/index.vue'] = viewsPlatformPermissionindex
    routerMap['/views/Platform/platformPermissions/index.vue'] = viewsPlatformplatformPermissionsindex
    routerMap['/views/Platform/Roles/index.vue'] = viewsPlatformRolesindex
    routerMap['/views/Platform/Users/index.vue'] = viewsPlatformUsersindex    
    routerMap['/views/Platform/Dept/index.vue'] = viewsPlatformDept   
    routerMap['/views/Platform/appPermissions/index.vue'] = viewsAppPermission    
    routerMap['/views/Platform/MessageType/index.vue'] = viewsMessageType;  
    routerMap['/views/Platform/UserMessage/index.vue'] = viewsUserMessage;
    routerMap['/views/BaseDoc/Product/index.vue'] = viewsBaseProduct;
    routerMap['/views/BaseDoc/Customer/index.vue'] = viewsBaseCustomer;   
    routerMap['/views/BaseDoc/Supplier/index.vue'] = viewsBaseSupplier;   
    routerMap['/views/BaseDoc/Process/index.vue'] = viewsBaseProcess;
    routerMap['/views/Project/Info/index.vue'] = viewsProjectInfo;
    routerMap['/views/Project/Scheduling/index.vue'] = viewsProjectScheduling;
    routerMap['/views/Project/Execution/index.vue'] = viewsProjectExecution;
    routerMap['/views/Project/Receive/index.vue'] = viewsProjectReceive;
    routerMap['/views/Project/Invoice/index.vue'] = viewsProjectInvoice;
    routerMap['/views/Customer/FollowUp/index.vue'] = viewsCustomerFollowUp;
    routerMap['/views/Customer/Systems/index.vue'] = viewsCustomerSystems;
    routerMap['/views/Customer/Resume/index.vue'] = viewsCustomerResume;
    routerMap['/views/Supplier/Resume/index.vue'] = viewsSupplierResume;
    routerMap['/views/Project/Finance/index.vue'] = viewsProjectFinance;
    routerMap['/views/File/index.vue'] = viewsFileCenter;
    routerMap['/views/Platform/PageConfig/index.vue'] = viewsPloatformPage;
    routerMap['/views/Report/Finance/index.vue'] = viewsReportFinance;
    routerMap['/views/Report/Customer/index.vue'] = viewsReportCustomer;
    routerMap['/views/Test/index.vue'] = viewsTest1;
    routerMap['/views/Certificate/CertificateList/index.vue'] = viewsCertificateList
    routerMap['/views/Certificate/CertificateAnalysis/index.vue'] = viewsCertificateAnalysis
    routerMap['/views/Report/WorkTime/index.vue'] = viewsReportWorktime
}


const actions = {
    generateRoutes({ commit }, roles) {
        CreateRouterMap();
        return new Promise(resolve => {
            let accessedRoutes
            accessedRoutes = asyncRoutes || []
            let userPermission = GetUserPermission()
            userPermission.forEach((v) => {
                accessedRoutes.push(v)
            })
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })
    }
}

function GetUserPermission() {
    let result = []
    for (let i = 0; i < store.getters.permission_user.length; i++) {
        let currMenu = GetMenu(store.getters.permission_user[i])
        result.push(currMenu)
    }
    
    return result;
}

function GetMenu(userPermise) {
    var result = {
        path: userPermise.path,
        component: routerMap[userPermise.component],
        name: userPermise.name,
        meta: userPermise.meta,
        alwaysShow: userPermise.alwaysShow,
        buttons: userPermise.buttons,
        children: null
    }
    if (userPermise.children && userPermise.children.length > 0) {
        GetChildren(result, userPermise.children)
    }

    return result
}

function GetChildren(parent, children) {
    let childList = []
    for (let i = 0; i < children.length; i++) {
        let currchild = {
            path: children[i].path,
            component: routerMap[children[i].component],
            name: children[i].name,
            meta: children[i].meta,
            buttons: children[i].buttons
            //children: null
        }
        if (children[i].children && children[i].children.length > 0) {
            GetChildren(currchild, children[i].children)
        }
        childList.push(currchild)
    }
    parent.children = childList
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
